﻿/**
 * ...
 * @author Sergey Mamyan
 */

package tx.squirrel.neko.core;

import neko.db.Connection;
import neko.db.Mysql;
import neko.db.Manager;
import neko.db.ResultSet;
import neko.db.Sqlite;
import neko.Lib;
import neko.Web;
import tx.squirrel.neko.core.model.Config;


class Database {
	
	public static var connection:Connection;
	
	public static function open() {
		switch (Config.DATABASE_TYPE) {
			case "SQLITE"	: connection = Sqlite.open(Config.DATABASE_NAME+".db");
			case "MYSQL"	: connection = Mysql.connect({
				host 		: Config.DATABASE_HOST,
				port 		: Config.DATABASE_PORT,
				user 		: Config.DATABASE_USER,
				pass 		: Config.DATABASE_PASS,
				database 	: Config.DATABASE_NAME,
				socket 		: null
			});
		}
		Manager.cnx = connection;
		Manager.initialize();
		load();
	}
	
	public static function escape(s:String):String{
		return connection.escape(s);
	}
	
	public static function quote(s:String):String{
		return connection.quote(s);
	}
	
	public static function lastInsertId():Int{
		return connection.lastInsertId();
	}
	
	public static function request(s:String):ResultSet{
		return connection.request(s);
	}
	
	public static function commit():Void {
		connection.commit();
	}
	
	public static function startTransaction():Void{
		connection.startTransaction();
	}
	
	public static function rollback():Void{
		connection.rollback();
	}
	
	public static function close():Void {
		connection.close();
	}
	
	private static function load():Void {
		//Config.set("MELLO","Grish Axper");
	}
	
}
